<?php
uses('model');

class scoreModel extends Model {

    function __construct() {
        parent::__construct();
    }

    function update($v) {
        $this->dbconnect();

        $fields['uid']      = $v['uid'];
        $fields['iid']      = $v['iid'];
        $fields['types']    = $v['types'];
        $fields['score']    = $v['score'];
        $fields['memo']     = $v['memo'];
        $fields['updated']  = time();

        $ok = $this->db->Insert($this->table['score'], $fields) ? 1 : 2;

        //更新用户表
        if ($ok == 1) $this->db->Update($this->table['users'], array('score'=>"{[+{$fields['score']}]}"), "id={$fields['uid']}");

        return $ok;
    }

    /**
     * 按 id 删除积分记录
     *
     * @param mixed $v
     * @access public
     * @return int 成功为 1 失败为 2
     */
    function del($v) {
        $this->dbconnect();

        $row = $this->db->GetRow($this->table['score'], 'uid, score', "id={$v['id']}");
        $ok = $this->db->Delete($this->table['score'], "id={$v['id']}") ? 1 : 2;

        if ($ok == 1) $this->db->Update($this->table['users'], array('score'=>"{[-{$row['score']}]}"), "id={$row['uid']}");

        return $ok;
    }

    /**
     * 按订单号删除积分记录
     *
     * @param int $iid 订单号
     * @access public
     * @return int 成功为 1 失败为 2
     */
    function delByiid($iid) {
        $this->dbconnect();

        $row = $this->db->GetRow($this->table['score'], 'uid, score', "iid={$iid}");
        $ok = $this->db->Delete($this->table['score'], "iid={$iid}") ? 1 : 2;

        if ($row['score'] && $ok == 1) $this->db->Update($this->table['users'], array('score'=>"{[-{$row['score']}]}"), "id={$row['uid']}");

        return $ok;
    }
}
?>
